#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "QHDX_main.h"

void FindStudentInfo()
{
	struct StudentInfo student;
	int FindNumber = 1;
	printf("\n***欢迎使用查询模式***\n");
	while(1)
	{
		printf("\n请输入您相查询的学生学号:>\n");
		if(scanf("%d", &FindNumber) != 1)
		{
			printf("您输入的学号有误，请重新输入！(退出输入0)");
			while(getchar() != '\n');
			continue;
		}
		
		if(FindNumber == 0) break;
		
		struct StudentInfo* B = FindStudent(FindNumber);
		if(B == NULL)
		{
			printf("没有找到'%d'该学号学生信息(0表示退出)", FindNumber);
			free(B);
			continue;
		}

		printf("\n******学生信息如下******\n");
		printf("\n学生姓名:%s",B->name);
		printf("\n学生学号:%d",B->number);
		printf("\n学生性别:%s",B->sex ==0 ? "女生":"男生");
		printf("\n语文成绩:%hd",B->grade.Chinese);
		printf("\n数学成绩:%hd",B->grade.Math);
		printf("\n英语成绩:%hd",B->grade.English);
		
		free(B);		

		printf("\n是否继续查询？(y/n)");
		getchar();
		if(getchar() != 'y') break;
	}
}

struct StudentInfo* FindStudent(int FindNumber)
{
	struct StudentInfo student;
	FILE * fp = fopen("StudentData","rb");
	while(fread(&student, sizeof(student), 1, fp))
	{
		if(student.number == FindNumber) 
		{
			struct StudentInfo* A =(struct StudentInfo*)malloc(sizeof(struct StudentInfo));
			memcpy(A, &student, sizeof(student));
			return A;
		}
	}

	fclose(fp);
}
